/**
  * <br>JAVACC DEMO 1.0<br>
  * @copy right dwusoft company All rights reserved.<br>
  * <br>
  * @Package com.apache.dict.impl
*/
package com.apache.dict.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.dict.entity.DataCate;
import com.apache.dict.manager.DataCateManager;
import com.apache.dict.service.AbstractBaseManager;
import com.apache.exception.BusinessException;

/**
  * description: 数据字典实体业务逻辑接口实现
  * @author Hou DaYu 创建时间：上午09:21:57
  */
public class DataCateManagerImpl extends AbstractBaseManager implements DataCateManager {

	protected final String entityName = "com.apache.dict.entity.DataCate";

	/* (non-Javadoc)
	 * @see com.apache.api.manager.BaseManager#saveInfo(com.apache.api.vo.ParamsVo)
	 */
	public String saveInfo(ParamsVo<DataCate> vo) throws BusinessException {
		DataCate dataCate = vo.getObj();
		String cateId = Validator.generate();
		dataCate.setCateId(cateId);
		String sysEname = Validator.getDefaultStr(dataCate.getSysEname(), "defult");
		dataCate.setCreateTime(System.currentTimeMillis());
		dataCate.setUpdateTime(System.currentTimeMillis());
		dataCate.setSysEname(sysEname);
		MethodParam param = new MethodParam("DataCate", "cate_" + cateId, "", entityName);
		param.setVaule(dataCate);
		if (dataCateDao.insert(param)) {
			return cateId;
		}
		return "";
	}

	/* (non-Javadoc)
	 * @see com.apache.api.manager.BaseManager#deleteInfo(com.apache.api.vo.ParamsVo)
	 */
	public boolean deleteInfo(ParamsVo<DataCate> vo) throws BusinessException {
		String dataCateId = vo.getInfoId();
		if (Validator.isNull(dataCateId))
			return false;

		MethodParam param = new MethodParam("ById", "cate_" + dataCateId, "", entityName);
		param.setInfoId(dataCateId);
		DataCate dataCate = (DataCate) dataCateDao.selectById(param);
		if (Validator.isEmpty(dataCate)) {
			return false;
		}
		param.setVaule(dataCate);
		param.setParams("cateId", dataCateId);
		param.setDelete(true);
		return dataCateDao.delete(param);
	}

	/* (non-Javadoc)
	 * @see com.apache.api.manager.BaseManager#editInfo(com.apache.api.vo.ParamsVo)
	 */
	public boolean editInfo(ParamsVo<DataCate> vo) throws BusinessException {
		DataCate dataCate = vo.getObj();
		if (Validator.isNotNull(dataCate.getCateId())) {
			dataCate.setUpdateTime(System.currentTimeMillis());
			MethodParam param = new MethodParam("DataCate", "cate_" + dataCate.getCateId(), "", entityName);
			param.setVaule(dataCate);
			return dataCateDao.edit(param);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.apache.api.manager.BaseManager#execute(com.apache.api.vo.ParamsVo)
	 */
	public Object execute(ParamsVo<DataCate> vo) {
		String methodKey = vo.getMethodKey();
		if ("checkCateEname".equalsIgnoreCase(methodKey)) {//判断科目标识是否唯一
			String cateEname = String.valueOf(vo.getParams("cateEname"));
			return checkCateEname(cateEname);
		} else {

		}
		return null;
	}

	/**
	 * 判断科目标识是否唯一
	 * @param eName 科目标识
	 * @return 存在返回false;不存在返回true
	 */
	private boolean checkCateEname(String cateEname) {
		boolean mark = true;
		String sql = dataCateDao.getSql(3);
		MethodParam param = new MethodParam("ByCateEname", "", sql, entityName);
		param.setParams("cateEname", cateEname);
		long count = dataCateDao.count(param);
		if (count > 0)
			return false;
		return mark;
	}

	/* (non-Javadoc)
	 * @see com.apache.api.manager.BaseManager#getInfoById(com.apache.api.vo.ParamsVo)
	 */
	public Object getInfoById(ParamsVo<DataCate> vo) {
		String dataCateId = vo.getInfoId();
		if (Validator.isNull(dataCateId))
			return null;
		MethodParam param = new MethodParam("ById", "cate_" + dataCateId, "", entityName);
		param.setInfoId(dataCateId);
		return dataCateDao.selectById(param);
	}

	/* (non-Javadoc)
	 * @see com.apache.api.manager.BaseManager#getList(com.apache.api.vo.ParamsVo)
	 */
	@SuppressWarnings("unchecked")
	public List<DataCate> getList(ParamsVo<DataCate> vo) {
		MethodParam param = setMethodParams(vo, 1, "cateCname asc");
		param.setParams("orderList", " order by cate_Cname asc");
		return dataCateDao.select(param);
	}

	public Page getPageInfo(ParamsVo<DataCate> vo) {
		MethodParam param = setMethodParams(vo, 1, "cateCname asc");
		param.setOrderby(" cate_Cname asc");
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = dataCateDao.pageSelect(param);
		return page;
	}

	/* (non-Javadoc)
	 * @see com.apache.api.manager.BaseManager#countInfo(com.apache.api.vo.ParamsVo)
	 */
	public long countInfo(ParamsVo<DataCate> vo) {
		MethodParam param = setMethodParams(vo, 2, "");
		return dataCateDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<DataCate> vo, int type, String orderby) {
		String cateEname = String.valueOf(vo.getParams("cateEname"));
		String cateCname = String.valueOf(vo.getParams("cateCname"));
		String cateStatus = String.valueOf(vo.getParams("cateStatus"));
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(dataCateDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		if (Validator.isNotNull(cateEname)) {
			sb.append("and cateEname like :cateEname");
			param.setParams("cateEname", "'%" + cateEname + "%' ");
		}
		if (Validator.isNotNull(cateCname)) {
			sb.append("and cateCname like :cateCname");
			param.setParams("cateCname", "'%" + cateCname + "%' ");
		}
		if (Validator.isNotNull(cateStatus)) {
			sb.append("and cateCname = :cateStatus");
			param.setParams("cateStatus", cateStatus);
		}
		String sysEname = String.valueOf(vo.getParams("sysEname"));
		if (Validator.isNotNull(sysEname)) {
			sb.append("and sysEname = :sysEname");
			param.setParams("sysEname", sysEname);
		}
		if (Validator.isNotNull(orderby)) {
			sb.append(" order by " + orderby);
		}
		param.setSqlStr(sb.toString());
		return param;
	}
}
